#!/bin/sh

################################################################################
##                                                                            ##
## Copyright (c) International Business Machines  Corp., 2005                 ##
##                                                                            ##
## This program is free software;  you can redistribute it and#or modify      ##
## it under the terms of the GNU General Public License as published by       ##
## the Free Software Foundation; either version 2 of the License, or          ##
## (at your option) any later version.                                        ##
##                                                                            ##
## This program is distributed in the hope that it will be useful, but        ##
## WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY ##
## or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License   ##
## for more details.                                                          ##
##                                                                            ##
## You should have received a copy of the GNU General Public License          ##
## along with this program;  if not, write to the Free Software               ##
## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA    ##
##                                                                            ##
##                                                                            ##
################################################################################
#
# File:
#   check_icmpv4_connectivity
#
# Description:
#   Check the ICMPv4 connectivity from a interface to an IPv4 address
#
# Arguments:
#   $1: source interface name
#   $2: destination IPv4 address
#
# Returns:
#   0: connectivity is good
#   1: connectivity is someting wrong
#
# Author:
#   Mitsuru Chinen <mitch@jp.ibm.com>
#
# History:
#   Oct 19 2005 - Created (Mitsuru Chinen)
#
#-----------------------------------------------------------------------
#Uncomment line below for debug output.
#trace_logic=${trace_logic:-"set -x"}
$trace_logic

# The max number of ICMP echo request
PING_MAX=10

# Check the arguments
if [ $# -ne 2 ]; then
    echo "Usage: $0 source_interface_name destionation_ipv4_address" >&2
    exit 1
fi
src_ifname=$1
dst_ipv4addr=$2

cnt=0
while [ $cnt -lt $PING_MAX ]; do
    cnt=`expr $cnt + 1`
    ping -I $src_ifname -c 1 $dst_ipv4addr -w 1 > /dev/null 2>&1
    if [ $? -eq 0 ]; then
	exit 0
    fi
    sleep 1
done

exit 1
